
#include "log.h"
#include "selinux.h"

#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>

int selinux_enforce_get() {
    int fd = 0, n = 0, ret = -1;
    char text[4] = {0};

    fd = open("/sys/fs/selinux/enforce", O_RDONLY);
    if (fd < 0) {
        LOGV("open, %d\n", errno);
    	if ( errno == EACCES )
    		return 0;

        return -1;
    }
    n = read(fd, text, sizeof(text));
    if (n < 0)
        LOGE("read()");
    close(fd);
    if (n <= 0)
        return -2;

    sscanf(text, "%d", &ret);
    LOGV("se_sscanf:%s, %d", text, ret);
    return ret;
}

int selinux_attr_set_priv() {
    int fd, n;

    fd = open("/proc/self/attr/current", O_WRONLY);
    if (fd < 0) {
        // LOGE("open()");
        return -1;
    }
    n = write(fd, "u:r:init:s0\n", 12);
    if (n < 0)
        LOGE("write()");
    close(fd);
    return 0;//n == 12 ? 0 : -1;
}

